package com.yanfan.data.controller.dashBoard;

import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.iot.service.IDeviceService;
import com.yanfan.iot.service.IFunctionLogService;
import com.yanfan.iot.tdengine.service.ILogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.TreeMap;

@Api(tags = "设备仪表盘")
@RestController
@RequestMapping("/iot/device/dashBoard")
public class DeviceDashBoardController {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IFunctionLogService functionLogService;

    @Autowired
    private ILogService deviceLogService;

    /**
     * 获取设备统计信息
     */
    @GetMapping(value = "/statistic")
    @ApiOperation("获取设备统计信息")
    public AjaxResult getDeviceStatistic() {
        return AjaxResult.success(deviceService.selectDashBoardDeviceStatistic());
    }

    /**
     * 获取设备交互统计信息
     */
    @GetMapping(value = "/functionStatistic")
    @ApiOperation("获取设备交互统计信息")
    public AjaxResult functionStatistic(Long limit, String type) {
        AjaxResult success = AjaxResult.success();
        Map<String, String> countMap = functionLogService.selectFunctionLogCount();
        Map<String, String> map = functionLogService.selectDashBoardFunctionStatistic();
        TreeMap<String, String> treeMap = new TreeMap<>(map);
        success.put("countMap", countMap);
        success.put("treeMap", treeMap);
        return success;
    }

    /**
     * 获取设备消息统计信息
     */
    @GetMapping(value = "/deviceLogYesterdayCount")
    @ApiOperation("获取设备消息统计信息")
    public AjaxResult selectDeviceLogYesterdayCount() {
        AjaxResult success = AjaxResult.success();
        Map<String, String> totalCountMap = deviceLogService.selectDeviceLogTotalCount();
        Map<String, String> yesterdayCountMap = deviceLogService.selectDeviceLogYesterdayCount();
        success.put("totalCountMap", totalCountMap);
        success.put("yesterdayCountMap", yesterdayCountMap);
        return success;
    }

    /**
     * 获取设备消息最近一小时统计信息
     */
    @PostMapping(value = "/deviceLogAnHourStatistic")
    @ApiOperation("获取设备消息最近一小时统计信息")
    public AjaxResult selectDeviceLogAnHourStatistic(@RequestBody Map<String, String> queryMap) {
        return AjaxResult.success(deviceLogService.selectDashBoardDeviceLogStatistic(queryMap));
    }

    /**
     * 获取设备消息折线图
     */
    @PostMapping(value = "/deviceLogStatistic")
    @ApiOperation("获取设备消息折线图")
    public AjaxResult selectDeviceLogStatistic(@RequestBody Map<String, String> queryMap) {
        return AjaxResult.success(deviceLogService.selectDashBoardDeviceLogStatistic(queryMap));
    }
}
